<?php
declare (strict_types = 1);

namespace app\middleware;

use think\facade\Cache;
use think\Response;

class HelloFilter
{
    /**
     * 处理请求
     *
     * @param \think\Request $request
     * @param \Closure       $next
     * @return \think\Response
     */
    public function handle($request, \Closure $next)
    {
        echo 'HelloFilter:before | ';
        $url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
        $cache = Cache::store('file');
        if ($cache->has($url)) {
            $content = $cache->get($url);
            $response = Response::create($content);
            echo 'HelloFilter:hit | ';
            return $response;
        }


        $response = $next($request);
        echo 'HelloFilter:after | ';
        $content = $response->getContent();
        $cache->set($url, $content,3600);
        return $response;
    }
}
